package com.main.accountpermissionsmanagement.domain.account.service;

import com.main.accountpermissionsmanagement.application.service.AccountService;
import com.main.accountpermissionsmanagement.domain.account.entity.Account;
import com.main.accountpermissionsmanagement.domain.account.repository.AccountRepository;
import com.main.accountpermissionsmanagement.infrastructure.util.AccountGenerator;
import com.main.accountpermissionsmanagement.interfaces.assembler.AccountAssembler;
import com.main.accountpermissionsmanagement.interfaces.dto.AddAccountDTO;
import com.main.accountpermissionsmanagement.interfaces.dto.LoginDTO;
import com.main.common.base.base.BaseAssemblerFactory;
import com.main.common.base.resp.RespBody;
import com.main.common.utils.ConfigUtil;
import com.main.common.utils.SaTokenUtil;
import jakarta.annotation.Resource;
import lombok.Synchronized;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    private final String password =  ConfigUtil.getProperty("Default.Login.Password");
    @Resource
    private AccountRepository accountRepository;
    @Resource
    private BaseAssemblerFactory baseAssemblerFactory;

    private final SaTokenUtil saTokenUtil = new SaTokenUtil();

    @Override
    @Synchronized
    public Account addAccount(Long userId, String name) {
        System.out.println("Add account to account management");
        Long maxNumber = accountRepository.getAccountNumberMax();
        AccountGenerator accountGenerator= new AccountGenerator();
        String number = accountGenerator.generateAccount(name,maxNumber);
        AccountAssembler registerAssembler = new AccountAssembler(baseAssemblerFactory);
        AddAccountDTO addAccountDTO = new AddAccountDTO();
        addAccountDTO.setUserId(userId);
        addAccountDTO.setNumber(number);
        addAccountDTO.setPassword(password);
        Account account = registerAssembler.toEntity(addAccountDTO);
        accountRepository.save(account);
        return account;
    }

    @Override
    public RespBody<List<Account>> login(LoginDTO loginDTO) {
        List<Account> accountList= accountRepository.findByNumber(loginDTO.getUsername());
        if(accountList.isEmpty()){
            List<Long> userIdList= accountRepository.findByPhone(loginDTO.getUsername());
            if(userIdList.isEmpty()){
                return RespBody.fail("账号不存在");
            }else if(userIdList.size()==1){
                accountList = accountRepository.findByUserId(userIdList.get(0));
                Account account = accountList.get(0);
                if(this.checkPassword(loginDTO.getPassword(),account.getPassword())){
                    saTokenUtil.Login(account.getNumber(),"pc");
                    return RespBody.ok(accountList);
                }else{
                    return RespBody.fail("密码不正确");
                }
            } else {
                accountList = accountRepository.findByUserIds(userIdList);
                return RespBody.ok(accountList);
            }
        } else if (accountList.size()==1) {
            Account account = accountList.get(0);
            if(this.checkPassword(loginDTO.getPassword(),account.getPassword())){
                saTokenUtil.Login(account.getNumber(),"pc");
                return RespBody.ok(accountList);
            }else{
                return RespBody.fail("密码不正确");
            }
        }else{
            //处理重复账号
            return null;
        }
    }

    private boolean checkPassword(String loginPassword,String password) {
        return loginPassword.equals(password);
    }

}
